﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Windows.Forms;

namespace ProjectShop
{
    public class ClassShop
    {
        private SqlConnection scKengee_IT_MIS;
        public DataTable dtShop = new DataTable();
        public ComboBox cbShopState = new ComboBox();
        public ComboBox cbShopLever = new ComboBox();
        public ComboBox cbCircuitTypes = new ComboBox();
        public ComboBox cbOwningCompany = new ComboBox();

        public ClassShop(SqlConnection scn)
        {
            scKengee_IT_MIS = scn;
            GetShopInfoItems();
        }

        public void ShopNewInitial()
        {
            string sql = @"select * from ShopInfo where 1=2";
            SqlDataAdapter sda = new SqlDataAdapter(sql, scKengee_IT_MIS);
            scKengee_IT_MIS.Open();
            sda.Fill(dtShop);
            scKengee_IT_MIS.Close();
            sda.Dispose();
            dtShop.Rows.Add();
        }

        private void GetShopInfoItems()
        {
            string sql = @"select * from ConstantValue where TableName='ShopInfo' order by FieldName,Weight";
            DataTable dtShopInfoItems = new DataTable();
            SqlDataAdapter sdaShopInfo = new SqlDataAdapter(sql, scKengee_IT_MIS);
            scKengee_IT_MIS.Open();
            sdaShopInfo.Fill(dtShopInfoItems);
            scKengee_IT_MIS.Close();
            sdaShopInfo.Dispose();

            for (int i = 0; i < dtShopInfoItems.Rows.Count; i++)
            {
                string sFieldName=dtShopInfoItems.Rows[i]["FieldName"].ToString();
                string sValue=dtShopInfoItems.Rows[i]["Value"].ToString();
                switch (sFieldName)
                {
                    case "cbShopState": cbShopState.Items.Add(sValue); break;
                    case "cbShopLever": cbShopLever.Items.Add(sValue); break;
                    case "cbCircuitTypes": cbCircuitTypes.Items.Add(sValue); break;
                    case "cbOwningCompany": cbOwningCompany.Items.Add(sValue); break;
                }
            }
        }

        public bool GetShop_FastCode(string FastCode)
        //获取门店信息
        {
            bool result = false;
            string sql = @"select * from shopinfo where tbFastCode=" + FastCode;
            SqlDataAdapter sdaShop = new SqlDataAdapter(sql, scKengee_IT_MIS);
            scKengee_IT_MIS.Open();
            sdaShop.Fill(dtShop);
            scKengee_IT_MIS.Close();
            sdaShop.Dispose();

            if (dtShop.Rows.Count == 1)
            {
                result = true;
            }
            return result;
        }

        public bool GetShop_ID(string aID)
        {
            bool result = false;
            string sql = @"select * from shopinfo where ID=" + aID;
            SqlDataAdapter sdaShop = new SqlDataAdapter(sql, scKengee_IT_MIS);
            scKengee_IT_MIS.Open();
            sdaShop.Fill(dtShop);
            scKengee_IT_MIS.Close();
            sdaShop.Dispose();

            if (dtShop.Rows.Count == 1)
            {
                result = true;
            }
            return result;
        }

        public int ShopAppend()
        //添加新的门店信息，返回值为影响行数，如果为1则成功，不为1则失败
        {
            return ProjectCommon.ClassCommon.ClassAppend(scKengee_IT_MIS, dtShop, "ShopInfo");
        }

        public int ShopModify()
        //门店信息修改，返回值为影响行数，如果为1则成功，不为1则失败
        {
            ShopModify_UpdateJobTask();
            return ProjectCommon.ClassCommon.ClassModify(scKengee_IT_MIS, dtShop, "ShopInfo");
        }

        public void ShopModify_UpdateJobTask()
        {
            ClassShop csTemp=new ClassShop(scKengee_IT_MIS);
            string ID=this.dtShop.Rows[0]["ID"].ToString();
            csTemp.GetShop_ID(ID);
            string OldShopFastCode=csTemp.dtShop.Rows[0]["tbFastCode"].ToString();
            
            string NewFastCode=this.dtShop.Rows[0]["tbFastCode"].ToString();

            if (OldShopFastCode.Equals(NewFastCode) == false)
            {
                string sql = "update JobTask set tbShopFastCode='" + NewFastCode + "' where tbShopFastCode='" + OldShopFastCode + "'";
                SqlCommand scUpdate = scKengee_IT_MIS.CreateCommand();
                scUpdate.CommandText = sql;
                scKengee_IT_MIS.Open();
                scUpdate.ExecuteNonQuery();
                scKengee_IT_MIS.Close();
            }
        }
    }
}
